package info.macnana.online.core.base;

import info.macnana.online.core.constant.Code;

import java.io.Serializable;
/**
 * author: zhengheng
 * github: https://github.com/macnana777
 * email: 517862340@qq.com
 * <p>
 * Date: 2017-08-01 14:15
 * Description:通用响应对象
 * Copyright(©) 2017 by zhengheng.
 */
public class ResponseResult<T> implements Serializable {

    private Integer code;
    private String msg;
    private T data;

    private ResponseResult(Integer code){
        this.code = code;
    }

    private ResponseResult(Integer code,String msg){
        this.code = code;
        this.msg = msg;
    }

    private ResponseResult(Integer code,T data){
        this.code = code;
        this.data = data;
    }

    private ResponseResult(Integer code,String msg,T data){
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    public static <T> ResponseResult<T> success(){
        return new ResponseResult<>(Code.SUCCESS.getResultCode());
    }

    public static <T> ResponseResult<T> success(String msg){
        return new ResponseResult<>(Code.SUCCESS.getResultCode(),msg);
    }

    public static <T> ResponseResult<T> success(T data){
        return new ResponseResult<T>(Code.SUCCESS.getResultCode(),data);
    }

    public static <T> ResponseResult<T> success(String msg,T data){
        return new ResponseResult<T>(Code.SUCCESS.getResultCode(),msg,data);
    }

    public static ResponseResult<Void> error(String msg){
        return new ResponseResult<>(Code.ERROR.getResultCode(),msg);
    }

    public static <T> ResponseResult<T> error(Code code){
        return new ResponseResult<>(code.getResultCode(),code.getMessage());
    }

    public static <T> ResponseResult<T> error(Code code,T data){
        return new ResponseResult<T>(code.getResultCode(),code.getMessage(),data);
    }


}
